package cn.edu.cug.cs.gtl.gsm.jdbc.postgres;
import cn.edu.cug.cs.gtl.gsm.item.MetaItem;
import cn.edu.cug.cs.gtl.util.StringUtils;

import java.io.IOException;
import java.sql.*;

public class MetaTable {
    private String META_TABLE_NAME;
    private String SQL_DROP_META_TABLE;
    private String SQL_CREATE_META_TABLE;
    private String SQL_DELETE_META_TABLE_DATA;
    private String SQL_SELECT_META_TABLE_DATA;
    private String SQL_DELETE_ALL_META_TABLE_DATA;
    private String SQL_INSERT_META_TABLE_DATA;
    private String SQL_UPDATE_META_TABLE_DATA;

    protected Connection connection;


    public MetaTable(Connection connection, String baseName) {
        this.connection = connection;
        META_TABLE_NAME=baseName+".rdb_sm_meta";
        SQL_DROP_META_TABLE= "DROP TABLE IF EXISTS "+META_TABLE_NAME;
        SQL_CREATE_META_TABLE= "CREATE TABLE " + META_TABLE_NAME + " ( base_name  VARCHAR(255) PRIMARY KEY, meta VARCHAR )";
        SQL_DELETE_META_TABLE_DATA= "DELETE FROM " + META_TABLE_NAME + " WHERE base_name = ?";
        SQL_SELECT_META_TABLE_DATA= "SELECT meta FROM " + META_TABLE_NAME + " WHERE base_name = ?";
        SQL_DELETE_ALL_META_TABLE_DATA= "DELETE FROM " + META_TABLE_NAME ;
        SQL_INSERT_META_TABLE_DATA= "INSERT INTO " + META_TABLE_NAME + " VALUES (?,?)";
        SQL_UPDATE_META_TABLE_DATA= "UPDATE " + META_TABLE_NAME + " SET meta = ? WHERE base_name=?";

    }


    public boolean create() throws SQLException{
        Statement statement = connection.createStatement();
        boolean r =  statement.execute(SQL_CREATE_META_TABLE);
        return r;
    }

    public boolean drop() throws SQLException {
        Statement statement = connection.createStatement();
        boolean r =  statement.execute(SQL_DROP_META_TABLE);
        return r;
    }

    public boolean delete(String baseName) throws SQLException{
        PreparedStatement statement = connection.prepareStatement(SQL_DELETE_META_TABLE_DATA);
        statement.setString(1,baseName);
        boolean r =  statement.execute();
        return r;
    }

    public boolean deleteAll() throws SQLException{
        Statement statement = connection.createStatement();
        boolean r =  statement.execute(SQL_DELETE_ALL_META_TABLE_DATA);
        return r;
    }

    public boolean insert(String baseName, MetaItem metaItem) throws SQLException, IOException {
        PreparedStatement statement = connection.prepareStatement(SQL_INSERT_META_TABLE_DATA);
        statement.setString(1,baseName);
        statement.setString(2,metaItem.storeToCompressString());
        boolean r =  statement.execute();
        return r;
    }

    public boolean update(String baseName, MetaItem metaItem) throws SQLException, IOException {
        PreparedStatement statement = connection.prepareStatement(SQL_UPDATE_META_TABLE_DATA);
        statement.setString(2,baseName);
        statement.setString(1,metaItem.storeToCompressString());
        boolean r =  statement.execute();
        return r;
    }

    public MetaItem select(String baseName) throws SQLException, IOException {
        PreparedStatement statement = connection.prepareStatement(SQL_SELECT_META_TABLE_DATA);
        statement.setString(1,baseName);
        ResultSet r =  statement.executeQuery();
        String compressZip = null;
        while(r.next()){
            compressZip = r.getString(1);
        }
        if(compressZip!=null) {
            MetaItem metaItem = new MetaItem(0, 0);
            metaItem.loadFromCompressString(compressZip);
            return metaItem;
        }
        else
            return null;
    }

    public boolean exists() throws SQLException{
        String []sa = StringUtils.split(META_TABLE_NAME,".");
        return PostgresStorageManager.exists(connection,sa[0],sa[1]);
    }
}
